﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Questions : System.Web.UI.Page
{
   // private string _answersOptions = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (Request.QueryString != null && Request.QueryString["msg"] != null)
                {
                   // showMessages.Visible = true;
                    //mmsg.InnerText = Request.QueryString["msg"].ToString();
                }
                string sortOrder = Enums.UserSorting.time.ToString();
                if (Request.QueryString != null && Request.QueryString["order"] != null)
                {
                    sortOrder = Request.QueryString["order"].ToString();
                }
                DrawPage((Enums.UserSorting)Enum.Parse(typeof(Enums.UserSorting), sortOrder));
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("Default.aspx?error=" + ex.Message);
        }
    }

    private void DrawPage(Enums.UserSorting sortorder)
    {
        ProxyWrapper.UserProxy.UserDetails objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["userObject"]);
        ProxyWrapper.QuestionsServiceWrapper _objQuestions = new ProxyWrapper.QuestionsServiceWrapper();
        List<ProxyWrapper.CommonProxyService.Questions> objQuestionsList = _objQuestions.GetQuestionsList(objUser.ClientId, objUser.HashCode);
        switch (sortorder)
        {
            case Enums.UserSorting.time:
                objQuestionsList = objQuestionsList.OrderByDescending(m => m.QuestionId).ToList<ProxyWrapper.CommonProxyService.Questions>();
                dateSorter.Style.Add("font-weight", "bolder");
                dateSorter.Style.Add("color", "#000");
                break;           
            case Enums.UserSorting.alpha:
                objQuestionsList = objQuestionsList.OrderBy(m => m.QuestionText).ToList<ProxyWrapper.CommonProxyService.Questions>();
                nameSorter.Style.Add("font-weight", "bolder");
                nameSorter.Style.Add("color", "#000");
                break;
            case Enums.UserSorting.service:
                objQuestionsList = objQuestionsList.OrderByDescending(m => m.ServicesList.Tables[0].Rows.Count).ToList<ProxyWrapper.CommonProxyService.Questions>();
                serviceSorter.Style.Add("font-weight", "bolder");
                serviceSorter.Style.Add("color", "#000");
                break;
            default:
                objQuestionsList = objQuestionsList.OrderByDescending(m => m.QuestionId).ToList<ProxyWrapper.CommonProxyService.Questions>();
                dateSorter.Style.Add("font-weight", "bolder");
                dateSorter.Style.Add("color", "#000");
                break;
        }
        if (objQuestionsList != null && objQuestionsList.Count > 0 && objQuestionsList[0].QuestionId > 0)
        {
            // objUser.
            questionCount.InnerText = "Showing 1 - " + objQuestionsList.Count.ToString() + " of " + objQuestionsList.Count.ToString() + " Questions";
            lstQuestion.DataSource = objQuestionsList;
            lstQuestion.DataBind();
        }
        else
        {
            lstQuestion.DataSource = new object[] { };
            lstQuestion.DataBind();
        }
    }

    public string GetAnswersOption(string questionId)
    {
        List<ProxyWrapper.CommonProxyService.Questions> objQuestionList = ((List<ProxyWrapper.CommonProxyService.Questions>)lstQuestion.DataSource);
        ProxyWrapper.CommonProxyService.Questions _Questions = null;
        if (objQuestionList!=null)
        {
            _Questions = objQuestionList.First(m => m.QuestionId.ToString() == questionId);
            if (_Questions!=null && _Questions.AnswerOptions != null && _Questions.AnswerOptions.Length > 0 && _Questions.AnswerOptions.Select(m=>m.Trim().Length>0).ToString().Length>0)
            {
                string csv = String.Join(",", _Questions.AnswerOptions.Select(x => x.ToString()).ToArray());
                if (csv.Trim().Length > 0)
                    return "<dt><img src='Images/answer16.png' class='attr' /></dt><dd title='Answers of the question.'>" + csv + "</dd>";
                else
                    return "";
            }
            else
                return "";
        }
        
        return "";
    }
    public string GetImage(string questionType)
    {
        questionType = questionType.Split('-')[0].Trim();
        string imageURL = "";
       
        switch (questionType)
        {
            case QuestionsTypes.ShortAnswer:
                imageURL = "Images/qu_letters.png";
                break;
            case QuestionsTypes.ShortAnswerBarcode:
                imageURL = "Images/qu_barcode.png";
                break;
            case QuestionsTypes.ShortAnswerNumeric:
                imageURL = "Images/qu_numbers.png";
                break;
            case QuestionsTypes.ShortAnswerBarcodeNumeric:
                imageURL = "Images/qu_barcodenumeric.png";
                break;
            case QuestionsTypes.MultipleChoiceSingleAnswer:
                imageURL = "Images/qu_radio.png";
                break;
            case QuestionsTypes.MultiChoiceMultipleAnswers:
                imageURL = "Images/qu_check.png";
                break;
            case QuestionsTypes.MultipleChoiceDropdown:
                imageURL = "Images/qu_dropdown.png";
                break;
            default:
                break;                
        }
        if(QuestionsTypes.MultipleChoiceSingleAnswer.Contains(questionType))
             imageURL = "Images/qu_radio.png";
         if(QuestionsTypes.MultiChoiceMultipleAnswers.Contains(questionType))
             imageURL = "Images/qu_check.png";
         if(QuestionsTypes.MultipleChoiceDropdown.Contains(questionType))
             imageURL = "Images/qu_dropdown.png";
         return imageURL;
    }
    public string GetServiceCount(string questionId)
    {
        List<ProxyWrapper.CommonProxyService.Questions> objQuestionList = ((List<ProxyWrapper.CommonProxyService.Questions>)lstQuestion.DataSource);
        DataTable dcServices = objQuestionList.First(m => m.QuestionId.ToString() == questionId)!=null ?objQuestionList.First(m => m.QuestionId.ToString() == questionId).ServicesList.Tables[0]:null;
        if (dcServices != null && dcServices.Rows.Count > 0)
            return "<span class='numberOfServices'>" + dcServices.Rows.Count.ToString() + " Services...</span>" + GetServiceDetails(dcServices);
        else
            return "<i>Not associated with any services.</i>";

    }

    private string GetServiceDetails(DataTable dcServices)
    {
        StringBuilder sb = new StringBuilder();
        if (dcServices != null)
        {
            sb.Append("<span class='serviceList' style='display: none;'>");
            for (int i = 0; i < dcServices.Rows.Count; i++)
            {
                sb.Append(" <a href='Services.aspx?serviceId=" + dcServices.Rows[i][0].ToString() + "'>" + dcServices.Rows[i][1].ToString() + "</a> ");
                sb.Append(",");
            }

        }
        if (sb != null && sb.Length > 0)
        {
            sb = sb.Remove(sb.Length - 1, 1);
        }
        sb.Append("</span>");
        sb.Append("    <a class='sClickToShow' style='text-decoration:underline;'>Click to show</a>");
        return sb.ToString();
    }
}